import PutstorageInfo from "@/components/putstorageInfo";
import {
    findAllType,
    findGoodsStatus,
    findImelListById,
    submitImelList,
    updataIntoState, updateIdStorageNum
} from '@/api/sales/salesPutStorageDetails'

export default {
    components: {PutstorageInfo},
    data() {
        return {
            disableNoteStr:'',
            putStotageInfo: {},
            goods: {},
            options: [],
            imelList: "",
            note: "",
            purchaseType: '',
            imelListLenth: 0,
        };
    },
    watch: {
        imelList(val) {
            if (val.length === 0) {
                return this.imelListLenth = 0
            }
            let zzz = val.split("\n")
            this.imelListLenth = zzz.length;
        },
    },
    methods: {
        limitationLength() {

            if (this.imelListLenth > this.goods.purchaseNum) {
                let zzz = this.imelList.split("\n")
                let replace = zzz.pop().toString().replace(",", "\n");
                this.imelList = replace
            }
        },
        async submitImelList(status) {
            let zzz = this.imelList.split("\n")
            await submitImelList(this.$route.params.rid, zzz).then(()=>{
                if (status) {
                    this.$notify({
                        title: '成功',
                        message: "添加成功",
                        type: 'success'
                    });

                    //执行订单如果全部录入就修改订单状态
                } else {
                    this.$notify({
                        title: '暂存',
                        message: '暂存成功'
                    });
                }
            })
            //更新订单录入状态
            await updataIntoState(this.goods.id)
            await updateIdStorageNum(this.goods.id)
            await findGoodsStatus(this.$route.params.id)
            this.putClose()
        },


        putClose() {
            //解决数据不刷新,路由守卫也可以解决
            window.location.href = '/putStorageDetails/' + this.$route.params.id
            // this.$router.push({
            //     path: `/putStorageDetails/` + this.$route.params.id
            // })
        },
        handleAvatarSuccess(res) {
            let resLength = res.split("\n")
            if (resLength.length > this.goods.purchaseNum) {
                this.$message.error('错了哦，串码写多了');
            } else {
                this.imelList = res
                this.imelListLenth = resLength.length
            }
        },
        disableNote(){
            return this.disableNoteStr = this.goods.intoState === 1 && this.goods.putStorageNum>=this.goods.purchaseNum
        }
    },
    async created() {
        //使用vuex减少不必要的重复查询
        if (this.$store.state.putStotageInfo !== undefined) {

            this.putStotageInfo = this.$store.state.putStotageInfo;
            this.purchaseType = this.$store.state.purchaseType
            this.goods = this.$store.state.obj;
            this.options = await findAllType();
            this.imelList = await findImelListById(this.$route.params.rid)
            this.purchaseType =this.putStotageInfo.purchaseType
            this.disableNote()
        } else {
            this.$router.push({
                path: `/putStorageDetails/` + this.$route.params.id,
            })
        }
    }
};
